<template>
  <div>
    <!-- app分页组件 2018/7/19 zhangQ -->
    <slot :data="row"></slot>
  </div>
</template>

<script>
  export default {
    name: 'index',
    data () {
      let row = []
      let currentRow = [] // 当前页的行
      let pagination = {
        pageNumber: 1,
        total: 0,
        pageSize: 10,
        maxPage: 0 // 最大页
      }
      let customParam = {} // 刷新用到的自定义函数,每次查询后参数制空
      let datas = {row, pagination, customParam, currentRow}
      return datas
    },
    props: {
      isStartLoad: {
        type: Boolean,
        default: true
      },
      api: {
        type: Function,
        default () {
          return Promise.reject(new Error('tablePage api接口错误'))
        }
      },
      /* 初始化查询参数 */
      param: {
        type: Object,
        default () {
          return {}
        }
      },
      /* 初始化查询 排序字段 */
      sortName: {
        type: String,
        default () {
          return ''
        }
      },
      /* 初始化查询 排序类型 */
      sortType: {
        type: String,
        default () {
          return ''
        }
      },
      /* 格式化函数 */
      formatMethod: {}
    },
    computed: {},
    methods: {
      /**
       * TODO(获得当前所有行的原始数据)
       * @auther zhangQ
       * @date 2018/3/22 17:48
       */
      getRows () {
        return this.rows
      },
      /**
       * 获取数据
       * @param pagination 分页数据
       */
      readLoad (type) {
        this.getDatas(this.getAjaxParam(), type).then((res) => {
          this.pagination.total = res.total
          this.pagination.maxPage = Math.ceil(res.total / this.pagination.pageSize)
          if (this.pagination.pageNumber === 1) {
            this.row = res.rows
          } else if (this.pagination.pageNumber > 1) {
            this.row = this.row.concat(res.rows)
          }
          this.currentRow = res.rows
        })
      },
      getDatas (param, type) {
        return new Promise((resolve, reject) => {
          this.api(param).then((response) => {
            let resData = response.data
            if (!resData.data) {
              if (type === 'dataRefresh') {
                console.log('dataRefresh')
                this.row = []
                this.currentRow = []
              }
              return Promise.reject(new Error('没有数据'))
            }
            console.log('this.formatMethod', this.formatMethod)
            if (this.formatMethod && typeof this.formatMethod === 'function') {
              resData.data.rows = this.formatMethod(resData.data.rows)
            }
            resolve(resData.data)
          }).catch((error) => {
            if (type === 'dataRefresh') {
              console.log('dataRefresh')
              this.row = []
              this.currentRow = []
            }
            console.log(error)
          })
        })
      },
      nextPage (param) {
        if (!param) {
          param = {}
        }
        this.pagination.pageNumber++
        if (this.pagination.pageNumber > this.pagination.maxPage) {
          this.$toast.warning('没有数据')
          this.pagination.pageNumber--
          return
        }
        this.getDatas(Object.assign(this.getAjaxParam(), param), 'nextPage').then(res => {
          this.pagination.total = res.total
          this.pagination.maxPage = Math.ceil(res.total / this.pagination.pageSize)
          if (this.pagination.pageNumber === 1) {
            this.row = res.rows
          } else if (this.pagination.pageNumber > 1) {
            this.row = this.row.concat(res.rows)
          }
          this.currentRow = res.rows
        }).catch()
      },
      /**
       * param参数 isFilter是否是筛选
       * */
      dataRefresh (param, isFilter = true) {
        if (!param) {
          param = {}
        }
        if (isFilter) {
          this.pagination.pageNumber = 1
          this.customParam = param
          this.readLoad('dataRefresh')
          return
        }
        let sendObjClone = Object.assign(this.getAjaxParam(), param)
        sendObjClone.pageSize = this.row.length
        sendObjClone.pageNumber = 1
        this.getDatas(sendObjClone, 'dataRefresh').then(res => {
          if (res.rows.length > 0) {
            this.pagination.total = res.total
            this.pagination.maxPage = Math.ceil(res.total / this.pagination.pageSize)
            // 计算当前页
            this.row = res.rows
            // 计算当前行
            let oldPageNumber = this.pagination.pageNumber
            let oldPageSize = this.pagination.pageSize
            let startInde = oldPageSize * (oldPageNumber - 1)
            this.currentRow = res.rows.slice(startInde, res.rows.length)
          }
        }).catch()
      },
      /**
       * 获取 ajax 参数
       * @returns {{pageNumber: *, pageSize: number, obj: (props.param|{type, default}|HTMLParamElement|*), sortName: *, sortOrder: string}}
       */
      getAjaxParam () {
        let type = 'desc'
        // if (this.sortType !== 'desc' && this.sortType !== 'asc') {
        //   if (this.sortType === 'ascending') {
        //     type = 'asc'
        //   }
        // } else {
        //   type = this.sortType
        // }
        let resObj = Object.assign({
          pageNumber: this.pagination.pageNumber,
          pageSize: this.pagination.pageSize,
          sortName: this.sortName,
          sortOrder: type
        }, this.param, this.customParam)
        // 每次获取参数完毕把customParam 制空
        this.customParam = {}
        return resObj
      }
    },
    created () {
    },
    mounted () {
      if (this.isStartLoad) {
        this.readLoad('readLoad')
      }
    },
    components: {}
  }
</script>

<style scoped>

</style>
